package com.example.studyllm.service.rag;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studyllm.entity.rag.RagDoc;
import com.example.studyllm.mapper.rag.RagDocMapper;
import com.example.studyllm.service.base.BaseServiceImpl;
import com.example.studyllm.utils.BeanUtils;
import com.example.studyllm.vo.base.PageResultVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;

/**
 * <p>
 *   service实现类
 * </p>
 *
 * @author kw
 * @since 2025-02-05 15:12:48.381354
 */
@Service
public class RagDocService extends BaseServiceImpl<RagDocMapper, RagDoc> {


    public Boolean addRagDoc(RagDoc ragDoc) {
        return this.save(ragDoc);
    }

    public Boolean updateRagDocById(RagDoc ragDoc) {
        return this.updateById(ragDoc);
    }

    public PageResultVO<RagDoc> getPage(RagDoc pageParam) {

        LambdaQueryWrapper<RagDoc> lambda = new QueryWrapper<RagDoc>().lambda();
        this.buildCondition(lambda, pageParam);
        lambda.orderBy(true, false, RagDoc::getId);               
        PageResultVO<RagDoc> resultPage = BeanUtils.changePage(RagDoc.class, this.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda));
        return resultPage;
    }
    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<RagDoc> lambda, RagDoc param) {
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getId()),RagDoc::getId, param.getId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCode()),RagDoc::getCode, param.getCode());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getKnowledgeId()),RagDoc::getKnowledgeId, param.getKnowledgeId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getName()),RagDoc::getName, param.getName());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getPath()),RagDoc::getPath, param.getPath());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUrl()),RagDoc::getUrl, param.getUrl());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getSource()),RagDoc::getSource, param.getSource());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getSize()),RagDoc::getSize, param.getSize());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getSliceNum()),RagDoc::getSliceNum, param.getSliceNum());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getSliceStatus()),RagDoc::getSliceStatus, param.getSliceStatus());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCtime()),RagDoc::getCtime, param.getCtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUtime()),RagDoc::getUtime, param.getUtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getDeleteFlag()),RagDoc::getDeleteFlag, param.getDeleteFlag());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCby()),RagDoc::getCby, param.getCby());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUby()),RagDoc::getUby, param.getUby());
        
    }
}